package org.adit.spring.hibernate.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.adit.spring.hibernate.dao.OrderServiceDao;
import org.adit.spring.hibernate.entity.OrderService;
import org.adit.spring.hibernate.infra.Restrito;
import org.adit.spring.hibernate.service.FileService;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Put;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.multipart.UploadedFile;
import br.com.caelum.vraptor.view.Results;

@Resource
public class OrderServicesController {

	private OrderServiceDao dao;
	private final Result result;
	private FileService fileService;

	public OrderServicesController(FileService fileService, OrderServiceDao dao, Result result) {
		this.dao = dao;
		this.fileService = fileService;
		this.result = result;
	}

	@Restrito
	@Post
	@Path("/orderServices/adicionar")
	public void adiciona(OrderService orderService) {
		dao.salva(orderService);
		result.redirectTo(OrderServicesController.class).lista();
	}

	@Restrito
	@Get
	@Path("/orderServices")
	public void formulario() {

	}

	@Restrito
	@Get
	@Path("/orderServices/{osName}")
	public List<OrderService> formulario(String osName) {
		return dao.carregaOrderServiceSemGeocodificacao(osName);
	}
	
	@Restrito
	@Get
	@Path("/orderServices/{id}")
	public OrderService edita(Long id) {
		return dao.carrega(id);
	}

	@Restrito
	@Put
	@Path("/orderServices/{orderService.id}")
	public void altera(OrderService orderService) {
		dao.atualiza(orderService);
		result.redirectTo(OrderServicesController.class).lista();
	}

	@Restrito
	@Delete
	@Path("/orderServices/{id}")
	public void remove(Long id) {
		OrderService orderService = dao.carrega(id);
		dao.remove(orderService);
		result.redirectTo(OrderServicesController.class).lista();
	}

	@Get
	@Path("/orderServices/lista")
	public List<OrderService> lista() {
		return dao.listaTudo();
	}

	public List<OrderService> busca(String nome) {
		result.include("nome", nome);
		return dao.busca(nome);
	}

	@Post
	@Path("/orderServices/arquivo")
	public void orderServiceFile(UploadedFile imagem, OrderService orderService) {
		if (imagem != null && imagem.getFileName().endsWith("csv")) {
			fileService.readString(imagem.getFile(), orderService);
		}
		result.redirectTo(OrderServicesController.class).formulario();
	}

	private String getString(InputStream is) {
		int size;
		char[] theChars = new char[] {};
		byte[] bytes;
		try {
			size = is.available();

			theChars = new char[size];
			bytes = new byte[size];

			is.read(bytes, 0, size);
			for (int i = 0; i < size;) {
				theChars[i] = (char) (bytes[i++] & 0xff);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return new String(theChars);
	}

	@Get
	@Path("/orderServices/busca.json")
	public void buscaJson(String q) {
		result.use(Results.json()).from(dao.busca(q)).exclude("id", "descricao").serialize();
	}
}